package app.android.com.wuandroidapp.test.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;

import java.util.ArrayList;

import app.android.com.utilscode.util.ToastUtils;
import app.android.com.wuandroidapp.R;
import app.android.com.wuandroidapp.library.activity.BaseNoTitleActivity;
import app.android.com.wuandroidapp.test.view.definedview.TitleBar;
import app.android.com.wuandroidapp.test.view.fragment.BookFragment;
import app.android.com.wuandroidapp.test.view.fragment.GameFragment;
import app.android.com.wuandroidapp.test.view.fragment.HomeFragment;
import app.android.com.wuandroidapp.test.view.fragment.MusicFragment;
import app.android.com.wuandroidapp.test.view.fragment.TvFragment;
import butterknife.BindView;
import butterknife.ButterKnife;

public class MainNavigationActivity extends BaseNoTitleActivity implements BottomNavigationBar.OnTabSelectedListener {
    @BindView(R.id.title_text)
    TextView titleText;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.title)
    RelativeLayout title;
    @BindView(R.id.titlebar)
    TitleBar titlebar;
    private ArrayList<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_navigation);
        ButterKnife.bind(this);

        titlebar.setOntitlebarOnClickListener(new TitleBar.titlebarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {
                ToastUtils.showLong("点击无效");
            }
        });
        titleText.setText("导航栏");
        BottomNavigationBar bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);
        //设置导航栏模式
//        Attribute: bnbMode Values: mode_default, mode_fixed, mode_shifting
//        Method: setMode() Values:MODE_DEFAULT, MODE_FIXED, MODE_SHIFTING
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        //设置导航栏背景模式
//        Attribute: background_style_default, background_style_static, background_style_ripple
//        Method:BACKGROUND_STYLE_DEFAULT,BACKGROUND_STYLE_STATIC, BACKGROUND_STYLE_RIPPLE
        bottomNavigationBar
                .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC
                );
        bottomNavigationBar.addItem(new BottomNavigationItem(R.mipmap.ic_home_white_24dp, "地图").setActiveColorResource(R.color.orange))
                .addItem(new BottomNavigationItem(R.mipmap.ic_book_white_24dp, "自定义View1").setActiveColorResource(R.color.teal))
                .addItem(new BottomNavigationItem(R.mipmap.ic_music_note_white_24dp, "自定义View2").setActiveColorResource(R.color.blue))
                .addItem(new BottomNavigationItem(R.mipmap.ic_tv_white_24dp, "自定义View3").setActiveColorResource(R.color.brown))
                .addItem(new BottomNavigationItem(R.mipmap.ic_videogame_asset_white_24dp, "自定义View4").setActiveColorResource(R.color.grey))
                .setFirstSelectedPosition(0)
                .initialise();

        fragments = getFragments();
        setDefaultFragment();
        bottomNavigationBar.setTabSelectedListener(this);
    }

    /**
     * 设置默认的
     */
    private void setDefaultFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        if (!fragments.get(0).isAdded()) {
            transaction.add(R.id.layFrame, fragments.get(0));
        }
        transaction.hide(fragments.get(0));
        transaction.show(fragments.get(0));
        transaction.commit();
    }

    private ArrayList<Fragment> getFragments() {
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(HomeFragment.newInstance("Home"));
        fragments.add(BookFragment.newInstance("Books"));
        fragments.add(MusicFragment.newInstance("Music"));
        fragments.add(TvFragment.newInstance("Movies & TV"));
        fragments.add(GameFragment.newInstance("Games"));
        return fragments;
    }

    int oldposition = 0;

    @Override
    public void onTabSelected(int position) {

        if (fragments != null) {
            if (position < fragments.size()) {
                FragmentManager fm = getSupportFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                Fragment fragment = fragments.get(position);
                if (!fragments.get(position).isAdded()) {
                    ft.add(R.id.layFrame, fragment);
                }
                //      ft.hide(fragments.get(oldposition));
                ft.show(fragment);
                ft.commit();
                // oldposition=position;
            }
        }

    }

    @Override
    public void onTabUnselected(int position) {
        if (fragments != null) {
            if (position < fragments.size()) {
                FragmentManager fm = getSupportFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                Fragment fragment = fragments.get(position);
                if (!fragments.get(position).isAdded()) {
                    ft.add(R.id.layFrame, fragment);
                }
                ft.hide(fragment);
                //    ft.show(fragment);
                ft.commit();
                //       oldposition=position;
            }
        }
    }

    @Override
    public void onTabReselected(int position) {

    }

    public void back(View v) {
        finish();
    }

}
